﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.IO.Compression;
using System.Text.RegularExpressions;

namespace DataCollectService
{
    /// <summary>
    /// 同步请求
    /// </summary>
    public class HttpHelper
    {
        public string Simple(string url)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = request.GetResponse();
            Stream resStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(resStream);
            string strfile = sr.ReadToEnd();
            return strfile;
        }




        /*
         //请求
        Framework.HttpHelper.HttpModel mol = new Framework.HttpHelper.HttpModel();
        mol.url = "http://localhost:42407/webApi/WebForm2.aspx";
        mol.Method = "post";
        mol.Params = new List<Framework.HttpHelper.HttpParam>() { 
            new Framework.HttpHelper.HttpParam(){ name="va1", value="11111111"},
            new Framework.HttpHelper.HttpParam(){ name="va2", value="22222222"},
            new Framework.HttpHelper.HttpParam(){ name="va3", value="333333"},
            new Framework.HttpHelper.HttpParam(){ name="va4", value="444444"},
            new Framework.HttpHelper.HttpParam(){ name="va5", value="55555"}
        };
        string returnStr=Framework.HttpHelper.SynchronousRequest.GetHttpData(mol);
         */
        /// <summary>
        /// 同步请求(内存缓存)
        /// </summary>
        /// <returns>返回已StringBuilder类型采集的信息字符串</returns>
        public  string GetHttpData(HttpModel model)
        {
            if (string.IsNullOrEmpty(model.url)) return "错误：缺少接口地址！";

            //没有参数则转换回GET方式
            model.Method = "POST";
            if (model.Params == null) model.Method = "GET";
            if (model.Params != null && model.Params.Count == 0) model.Method = "GET";

            string strResult = "";
            HttpWebResponse res = null;
            HttpWebRequest req = null;
            StreamReader sr = null;
            try
            {
                req = (HttpWebRequest)WebRequest.Create(model.url);

                //最大连接数
                req.ServicePoint.ConnectionLimit = 1024;
                req.ContentType = model.ContentType;
                req.AllowAutoRedirect = model.AllowAutoRedirect;
                req.UserAgent = model.UserAgent;
                req.KeepAlive = model.KeepAlive;
                //req.ProtocolVersion = HttpVersion.Version10;
                req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
                if (model.cookieContainer != null)
                    req.CookieContainer = model.cookieContainer;
                //可接收压缩信息
                req.Headers.Add("Accept-Language", "zh-cn");
                req.Headers.Add("x-charset", "utf-8");
                req.Headers.Add("Accept-Encoding", "gzip,deflate");

                if (model.Headers != null)
                {
                    foreach (var header in model.Headers)
                    {
                        req.Headers.Add(header.Key, header.Value);
                    }
                }

                //提交方式
                req.Method = model.Method.ToUpper();
                #region post提交，数据传递
                if (model.Method.ToUpper() == "POST")
                {
                    //参数
                    StringBuilder parStr = new StringBuilder();
                    #region 组合参数
                    if (model.Params != null && model.Params.Count > 0)
                    {
                        //遍历所有参数
                        foreach (HttpParam par in model.Params)
                        {
                            parStr.Append(par.name);
                            parStr.Append("=");
                            parStr.Append(System.Web.HttpUtility.UrlEncode(par.value.ToString()));
                            parStr.Append("&");
                        }
                        //是否有多余符号
                        if (parStr.ToString().EndsWith("&")) parStr = parStr.Remove(parStr.Length - 1, 1);
                    }
                    #endregion
                    //转换成字节
                    byte[] SomeBytes = model.encoding.GetBytes(parStr.ToString());
                    //byte[] SomeBytes = new ASCIIEncoding().GetBytes(parStr.ToString());
                    //内容长度
                    req.ContentLength = SomeBytes.Length;

                    // 提交请求数据 
                    using (Stream newStream = req.GetRequestStream())
                    {
                        newStream.Write(SomeBytes, 0, SomeBytes.Length);
                        newStream.Close();
                    }
                }
                #endregion
                //接收返回参数
                res = (HttpWebResponse)req.GetResponse();

                if (res.Headers.Keys.Count > 0)
                {
                    //string cookie = res.Headers["set-cookie"].ToString();
                }

                //接收到内容是否有gzip压缩
                if (res.ContentEncoding != null && res.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                {
                    //开始读取流并设置编码方式
                    using (StreamReader reader = new StreamReader(new GZipStream(res.GetResponseStream(), CompressionMode.Decompress), model.encoding))
                    {
                        strResult = reader.ReadToEnd();
                    }
                }
                else
                {
                    //开始读取流并设置编码方式
                    using (StreamReader reader = new StreamReader(res.GetResponseStream(), model.encoding))
                    {
                        strResult = reader.ReadToEnd();
                    }
                }
            }
            catch (Exception e)
            {
                strResult = e.ToString();
            }
            finally
            {
                if (sr != null) sr.Close();
                if (res != null) res.Close();
                if (req != null) req.Abort();
            }

            return strResult;
        }

        public  void submit(string url)
        {
            HttpWebResponse res = null;
            HttpWebRequest req = null;
            try
            {
                req = (HttpWebRequest)WebRequest.Create(url);

                req.Method = "get";
                req.KeepAlive = false;
                req.ContentType = "application/x-www-form-urlencoded";

                //接收返回参数
                res = (HttpWebResponse)req.GetResponse();
            }
            catch (Exception e)
            {
                //Log.SaveNote(e.ToString());
            }
            finally
            {
                if (res != null)
                {
                    res.Close();
                }
            }
        }
        //默认的编码
        public  Encoding encoding = Encoding.Default;
        //HttpWebRequest对象用来发起请求
        public  HttpWebRequest request = null;
        //获取影响流的数据对象
        private  HttpWebResponse response = null;
        public  string cookie = "";
        public  Boolean isToLower = false;
        //读取流的对象
        private  StreamReader reader = null;
        //需要返回的数据对象
        private  string returnData = "String Error";

        public  string GetPostData(Dictionary<string, string> requestParams)
        {
            string postData = "";
            foreach (var item in requestParams)
            {
                postData += item.Key + "=" + item.Value + "&";
            }
            if (postData.Length > 0)
            {
                postData = postData.Substring(0, postData.Length - 1);
            }
            return postData;
        }

        /// <summary>    
        /// 传入一个正确或不正确的URl，返回正确的URL
        /// </summary>    
        /// <param name="URL">url</param>   
        /// <returns>
        /// </returns>    
        public  string GetUrl(string URL)
        {
            if (!(URL.Contains("http://") || URL.Contains("https://")))
            {
                URL = "http://" + URL;
            }
            return URL;
        }

        /// <summary>
        /// 为请求准备参数
        /// </summary>
        /// <param name="_URL">请求的URL地址</param>
        /// <param name="_Method">请求方式Get或者Post</param>
        /// <param name="_Accept">Accept</param>
        /// <param name="_ContentType">ContentType返回类型</param>
        /// <param name="_UserAgent">UserAgent客户端的访问类型，包括浏览器版本和操作系统信息</param>
        /// <param name="_Encoding">读取数据时的编码方式</param>
        /// <param name="_Header">头部信息</param>
        private  void SetRequest(string _URL, string _Method, string _Accept, string _ContentType, string _UserAgent, Encoding _Encoding, Dictionary<string, object> _Header = null, int _ContentLength = 0)
        {
            //初始化对像，并设置请求的URL地址
            request = (HttpWebRequest)WebRequest.Create(GetUrl(_URL));
            //请求方式Get或者Post
            request.Method = _Method;
            //Accept
            request.Accept = _Accept;
            //ContentType返回类型
            request.ContentType = _ContentType;
            //UserAgent客户端的访问类型，包括浏览器版本和操作系统信息
            request.UserAgent = _UserAgent;
            //c.Add(new Cookie("FG", "1"));
            //c.Add(new Cookie("PSTM", "1449821349"));
            //c.Add(new Cookie("BIDUPSID", "76BC7B6EA1B68A8F09151708979ABF0A"));
            //c.Add(new Cookie("BD_UPN", "12314353"));
            //c.Add(new Cookie("H_PS_645EC", "70feJBIWzPXMTbQCx9%2Bg2RL1Zmc3ociqGu7pmAnR6%2B3r01CwHyDtnWIKSGc"));
            //request.CookieContainer = c;
            if (_ContentLength != 0)
            {
                request.ContentLength = _ContentLength;
            }
            if (_Header != null && _Header.ContainsKey("date"))
            {
                request.Date = DateTime.Parse(_Header["date"].ToString()).ToUniversalTime();
            }
            //添加头部信息
            if (_Header != null)
            {
                foreach (var item in _Header)
                {
                    if (item.Key != "date")
                    {
                        request.Headers.Add(item.Key, item.Value.ToString());
                    }
                }
            }
            //读取数据时的编码方式
            encoding = _Encoding;
        }

        /// <summary>
        /// 4.0以下.net版本取水运
        /// </summary>
        /// <param name="streamResponse"></param>
        private  MemoryStream GetMemoryStream(Stream streamResponse)
        {
            MemoryStream _stream = new MemoryStream();
            int Length = 256;
            Byte[] buffer = new Byte[Length];
            int bytesRead = streamResponse.Read(buffer, 0, Length);
            // write the required bytes  
            while (bytesRead > 0)
            {
                _stream.Write(buffer, 0, bytesRead);
                bytesRead = streamResponse.Read(buffer, 0, Length);
            }
            return _stream;
        }

        /// <summary>
        /// 根据相传入的数据，得到相应页面数据
        /// </summary>
        /// <param name="strPostdata">传入的数据Post方式,get方式传NUll或者空字符串都可以</param>
        /// <returns>string类型的响应数据</returns>
        private  string GetHttpRequestData(string strPostdata)
        {
            try
            {
                //支持跳转页面，查询结果将是跳转后的页面
                request.AllowAutoRedirect = false;

                
                //验证在得到结果时是否有传入数据
                if (!string.IsNullOrEmpty(strPostdata) && request.Method.Trim().ToLower().Contains("post"))
                {
                    //byte[] buffer = encoding.GetBytes(strPostdata);
                    //request.ContentLength = buffer.Length;
                    //request.GetRequestStream().Write();
                    StreamWriter requestStream = new StreamWriter(request.GetRequestStream());
                    requestStream.Write(strPostdata);
                    requestStream.Close();
                }

                ////最大连接数
                request.ServicePoint.ConnectionLimit = 1024;
                //response = (HttpWebResponse)request.GetResponse()
                #region 得到请求的response

                using (response = (HttpWebResponse)request.GetResponse())
                {
                    try
                    {
                        cookie = response.Headers["set-cookie"].ToString();
                    }
                    catch (Exception)
                    {

                    }
                    //从这里开始我们要无视编码了
                    if (encoding == null)
                    {
                        MemoryStream _stream = new MemoryStream();
                        if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            //开始读取流并设置编码方式
                            //new GZipStream(response.GetResponseStream(), CompressionMode.Decompress).CopyTo(_stream, 10240);

                            //.net4.0以下写法
                            _stream = GetMemoryStream(response.GetResponseStream());
                        }
                        else
                        {
                            //response.GetResponseStream().CopyTo(_stream, 10240);
                            // .net4.0以下写法
                            _stream = GetMemoryStream(response.GetResponseStream());
                        }
                        byte[] RawResponse = _stream.ToArray();
                        string temp = Encoding.Default.GetString(RawResponse, 0, RawResponse.Length);
                        //<meta(.*?)charset([\s]?)=[^>](.*?)>
                        Match meta = Regex.Match(temp, "<meta([^<]*)charset=([^<]*)[\"']", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                        string charter = (meta.Groups.Count > 2) ? meta.Groups[2].Value : string.Empty;
                        charter = charter.Replace("\"", string.Empty).Replace("'", string.Empty).Replace(";", string.Empty);
                        if (charter.Length > 0)
                        {
                            charter = charter.ToLower().Replace("iso-8859-1", "gbk");
                            encoding = Encoding.GetEncoding(charter);
                        }
                        else
                        {
                            if (response.CharacterSet.ToLower().Trim() == "iso-8859-1")
                            {
                                encoding = Encoding.GetEncoding("gbk");
                            }
                            else
                            {
                                if (string.IsNullOrEmpty(response.CharacterSet.Trim()))
                                {
                                    encoding = Encoding.UTF8;
                                }
                                else
                                {
                                    encoding = Encoding.GetEncoding(response.CharacterSet);
                                }
                            }
                        }
                        returnData = encoding.GetString(RawResponse);
                    }
                    else
                    {
                        if (response.ContentEncoding != null && response.ContentEncoding.Equals("gzip", StringComparison.InvariantCultureIgnoreCase))
                        {
                            //开始读取流并设置编码方式
                            using (reader = new StreamReader(new GZipStream(response.GetResponseStream(), CompressionMode.Decompress), encoding))
                            {
                                returnData = reader.ReadToEnd();
                            }
                        }
                        else
                        {
                            //开始读取流并设置编码方式
                            using (reader = new StreamReader(response.GetResponseStream(), encoding))
                            {
                                returnData = reader.ReadToEnd();
                            }
                        }
                    }
                }

                #endregion
            }
            catch (WebException ex)
            {
                //这里是在发生异常时返回的错误信息
                returnData = "";
                response = (HttpWebResponse)ex.Response;
            }
            if (isToLower)
            {
                returnData = returnData.ToLower();
            }
            return returnData;
        }

        ///<summary>
        ///采用https协议Post方式访问网络,根据传入的URl地址，得到响应的数据字符串。
        ///</summary>
        ///<param name="URL">url地址</param>
        ///<param name="strPostdata">Post发送的数据</param>
        ///<param name="objencoding">编码方式例如：System.Text.Encoding.UTF8;</param>
        ///<returns>String类型的数据</returns>
        public  string GetHtml(string URL, string strPostdata, Encoding objencoding, Dictionary<string, object> header = null)
        {
            //准备参数
            SetRequest(URL, "post", "*/*", "application/x-www-form-urlencoded", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)", objencoding, header);
            //调用专门读取数据的类
            return GetHttpRequestData(strPostdata);
        }

        ///<summary>
        ///采用https协议GET方式访问网络,根据传入的URl地址，得到响应的数据字符串。
        ///</summary>
        ///<param name="URL">url地址</param>
        ///<param name="objencoding">编码方式例如：System.Text.Encoding.UTF8;</param>
        ///<returns>String类型的数据</returns>
        public  string GetHtml(string URL, Encoding objencoding)
        {
            string ua1 = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36";
            //string ua2 = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
            //准备参数
            SetRequest(URL, "GET", "text/html, application/xhtml+xml, */*", "text/html", ua1, objencoding);
            //request.GetRequestStream();
            //调用专门读取数据的类
            return GetHttpRequestData("");
            //return null;
            //response = (HttpWebResponse)request.GetResponse()
        }




        public string GetHtmlRaw(string URL, string strPostdata, Encoding objencoding, Dictionary<string, object> header = null)
        {
            //准备参数
            SetRequest(URL, "post", "*/*", "application/json;charset=UTF-8", "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)", objencoding, header);
            //调用专门读取数据的类
            return GetHttpRequestData(strPostdata);
        }
    }

    #region 实体参数
    /// <summary>
    /// 传入的实体类
    /// </summary>
    [Serializable]
    public class HttpModel
    {
        private string _cacheName = "";
        /// <summary>
        /// 缓存名称
        /// </summary>
        public string cacheName
        {
            get
            {
                return _cacheName;
            }
            set
            {
                _cacheName = value;
            }
        }

        private int _cacheTime = 0;
        /// <summary>
        /// 缓存时间（秒）
        /// </summary>
        public int cacheTime
        {
            get
            {
                return _cacheTime;
            }
            set
            {
                _cacheTime = value;
            }
        }
        private CookieContainer _cookieContainer = null;
        /// <summary>
        /// cookie信息
        /// </summary>
        public CookieContainer cookieContainer
        {
            get
            {
                return _cookieContainer;
            }
            set
            {
                _cookieContainer = value;
            }
        }

        private string _apiName = "urlApi";
        /// <summary>
        /// 接口在appSettings节点的名称
        /// </summary>
        public string apiName
        {
            get
            {
                return _apiName;
            }
            set
            {
                _apiName = value;
            }
        }
        private string _url = "";
        /// <summary>
        /// 需要提交的url地址
        /// </summary>
        public string url
        {
            get
            {
                if (string.IsNullOrEmpty(_url) && System.Configuration.ConfigurationManager.AppSettings[apiName] != null)
                {
                    _url = System.Configuration.ConfigurationManager.AppSettings[apiName].ToString();
                }
                return _url;
            }
            set
            {
                _url = value;
            }
        }

        /// <summary>
        /// 代理信息  默认 Mozilla/4.0
        /// </summary>
        public string UserAgent
        {
            get
            {
                return _UserAgent;
            }
            set
            {
                _UserAgent = value;
            }
        }
        private string _UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)";

        /// <summary>
        /// 提交的方式 默认GET方法
        /// </summary>
        public string Method
        {
            get
            {
                return _Method;
            }
            set
            {
                _Method = value;
            }
        }
        private string _Method = "GET";

        /// <summary>
        /// 提交的内容类型 默认：application/x-www-form-urlencoded
        /// </summary>
        public string ContentType
        {
            get
            {
                return _ContentType;
            }
            set
            {
                _ContentType = value;
            }
        }
        private string _ContentType = "application/x-www-form-urlencoded";

        /// <summary>
        /// 编码 默认UTF8 编码
        /// </summary>
        public Encoding encoding
        {
            get
            {
                return _encoding;
            }
            set
            {
                _encoding = value;
            }
        }
        private Encoding _encoding = Encoding.UTF8;


        /// <summary>
        /// 参数跟参数值
        /// </summary>
        public List<HttpParam> Params { get; set; }

        /// <summary>
        /// 保持持久连接  默认true开启
        /// （1）如果内存和CPU都足够，开启和关闭KeepAlive对性能影响不大。
        /// （2）如果考虑服务器压力，如果是静态页面，大量的调用js或者图片的话，建议开启KeepAlive；如果是动态网页，建议关闭KeepAlive。
        /// </summary>
        public bool KeepAlive
        {
            get
            {
                return _KeepAlive;
            }
            set
            {
                _KeepAlive = value;
            }
        }
        private bool _KeepAlive = true;

        /// <summary>
        /// Internet资源重定向  默认false
        /// 请求是否跟随重定向响应
        /// </summary>
        public bool AllowAutoRedirect
        {
            get
            {
                return _AllowAutoRedirect;
            }
            set
            {
                _AllowAutoRedirect = value;
            }
        }
        private bool _AllowAutoRedirect = false;

        /// <summary>
        /// 额外的头部信息
        /// </summary>
        public Dictionary<string, string> Headers { get; set; }
    }

    /// <summary>
    /// 参数
    /// </summary>
    [Serializable]
    public class HttpParam
    {
        public string name { get; set; }
        public object value { get; set; }
    }
    #endregion
}
